import config from '@/config.js'
import storage from '@/utils/storage.js'
import constant from '@/utils/constant.js'
import {
	authLogin,
	smCodeLogin,
	logout,
	login
} from '@/api/login.js'
import {
	getToken,
	setToken,
	removeToken
} from '@/utils/auth.js'
import {
	info
} from '@/api/personal.js'


const user = {
	state: {
		token: getToken(),
		name: storage.get(constant.name),
		avatar: storage.get(constant.avatar),
		userId: storage.get(constant.userId),
		coachId: storage.get(constant.coachId),
		info: storage.get(constant.info),
		userCard: storage.get(constant.userCard),
	},

	mutations: {
		SET_TOKEN: (state, token) => {
			state.token = token
		},
		SET_INFO: (state, info) => {
			state.info = info
			storage.set(constant.info, info)
		},
		SET_USERCARD: (state, userCard) => {
			state.userCard = userCard
			storage.set(constant.userCard, userCard)
		},
		SET_NAME: (state, name) => {
			state.name = name
			storage.set(constant.name, name)
		},
		SET_AVATAR: (state, avatar) => {
			state.avatar = avatar
			storage.set(constant.avatar, avatar)
		},
		SET_USERID: (state, userId) => {
			state.userId = userId
			storage.set(constant.userId, userId)
		},
		SET_COACHID: (state, coachId) => {
			state.coachId = coachId
			storage.set(constant.coachId, coachId)
		},

	},

	actions: {
		// 登录
		Login({
			commit
		}, userInfo) {
			const username = userInfo.account
			const password = userInfo.pwd
			return new Promise((resolve, reject) => {
				login(username, password).then(resp => {
					setToken(resp.token)
					commit('SET_TOKEN', resp.token)
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		},

		// 授权登录
		AuthLogin({
			commit
		}, {
			loginCode,
			phoneCode
		}) {
			return new Promise((resolve, reject) => {
				authLogin(loginCode, phoneCode).then(respones => {
					if (respones.token) {
						setToken(respones.token)
						commit('SET_TOKEN', respones.token)
					}
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		},
		/**
		 * 短信验证码登录
		 */
		SmCodeLogin({
			commit
		}, {
			loginCode,
			phone,
			code
		}) {
			return new Promise((resolve, reject) => {
				smCodeLogin(loginCode, phone, code).then(respones => {
					if (respones.token) {
						setToken(respones.token)
						commit('SET_TOKEN', respones.token)
					}
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		},

		// 获取用户信息
		GetInfo({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				info().then(res => {
					const user = res.data.user
					//教练角色设置
					user.role = "User"
					const coach = res.data.coach || {}
					const userCard =res.data.userCard || {}
					// && coach.status === 1
					if (Object.keys(coach).length > 0) {
						user.role = "Coach"
						commit('SET_COACHID', coach.id)
					}
					if (Object.keys(userCard).length > 0) {
						commit('SET_USERCARD', userCard)
					}
					commit('SET_NAME', user.nickName)
					commit('SET_AVATAR', user.avatar)
					commit('SET_USERID', user.userId)
					commit('SET_INFO', {
						name: user.nickName,
						avatar: user.avatar,
						userId: user.userId,
						phone: user.phonenumber,
					})
					
					resolve({
						user,
						coach
					})
				}).catch(error => {
					reject(error)
				})
			})
		},

		// 退出系统
		LogOut({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				logout(state.token).then(() => {
					commit('SET_TOKEN', '')
					removeToken()
					storage.clean()
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		}


	}
}

export default user